<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import {userInfoStore} from '@/stor/userInfo.js'
import { postApi } from '@/axios/http';
const userStore = userInfoStore();
const userInfo = ref(userStore.info);
const rules={
    nickname:[{request:true,message:'请输入用户名昵称',trigger:'blur'},
             {pattern: /^\S{2,10}$/,message:'昵称必须是2-10位非空字符串',trigger:'blur'}
            ],
    email:[{request:true,message:'请输入用户邮箱',trigger:'blur'},
            {type:'email',message:'邮箱格式不正确',trigger:'blur'} 
        ],
    age:[{request:true,message:'请输入年龄',trigger:'blur'},
         {pattern:/^(?:[1-9]\d?|1[0-2]\d|14[0-1])$/ ,message:'请输入正确的年龄',trigger:'blur'}],
   phone:[{request:true,message:'请输入手机号',trigger:'blur'},
          {pattern:/^1(3|4|5|6|7|8|9)\d{9}$/,message:'手机号格式不正确',trigger:'blur'}]
}
//修改用户信息
const updateUserInfo = ()=>{
    const header = "application/json"
    postApi("/user/update",userInfo.value,header).then(res=>{
        if(res.code === 0){
           ElMessage.success(res.message?res.message:'修改成功')
        }else{
            ElMessage.error(res.message)
        }
    }).catch(error=>{
        ElMessage.error(error)
    });
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span><strong>基本资料</strong></span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="登录名称">
                        <el-input v-model="userInfo.username" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="用户昵称" prop="nickname">
                        <el-input v-model="userInfo.nickname"></el-input>
                    </el-form-item>
                    <el-form-item  label="性别" prop="gender">
                        <el-radio-group v-model="userInfo.gender">
                            <el-radio value="1" size="large">男</el-radio>
                            <el-radio value="2" size="large">女</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="用户年龄" prop="age">
                        <el-input v-model="userInfo.age" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="用户邮箱" prop="email">
                        <el-input v-model="userInfo.email"></el-input>
                    </el-form-item>
                    <el-form-item label="用户手机号" prop="phone">
                        <el-input v-model="userInfo.phone" type="phone"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo()">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card> 
    
</template>